package com.kd.juhaowan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.flyjingfish.shapeimageviewlib.ShapeImageView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.hjq.toast.ToastUtils;
import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.LoginApi;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.model.LoginUser;
import com.kd.juhaowan.utils.GlideHelper;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ChangeAccountActivity extends BaseActivity {


    @BindView(R.id.swichAccount_avatar)
    ShapeImageView iconView;
    @BindView(R.id.swichAccount_username)
    TextView userNameView;

    private String avatar,userName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        avatar = getIntent().getStringExtra("avatar");
        userName = getIntent().getStringExtra("username");
        GlideHelper.loadGoodsCover(Glide.with(iconView),iconView,avatar);
        userNameView.setText(userName);
    }

    @Override
    protected void reInitDate() {

    }

    LoginUser myuser =null;

    @OnClick(R.id.login_confirm)
    public void onConfirmClick(){

        List<LoginUser> infos = manager.readAllUserAccountInfo();
        for (LoginUser info:infos) {
                if(info.getAccount().equals(userName)&&!info.isPhoneAccount()){
                    myuser = info;
                   break;
                }
        }

        if(myuser!=null){
            EasyHttp.post(this).api(new LoginApi(myuser.getAccount(),myuser.getPassword(),"account")).request(new HttpCallbackProxy<HttpData<LoginUser>>(this){
                @Override
                public void onHttpSuccess(HttpData<LoginUser> result, boolean cache) {
                    super.onHttpSuccess(result, cache);
//                        LogUtil.debugLog(result.getData().getAccess_token());
//                        Toast.makeText(AccountLoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                    ToastUtils.show("登录成功");
                    LoginUser user = result.getData();
                    user.setAccount(myuser.getAccount());
                    user.setPassword(myuser.getPassword());
                    user.setPhoneAccount(false);
                    manager.saveUserAccount(user);
                    manager.writeAutoLogin(true);
                    manager.writeLastLoginType(1);
                    manager.saveCurrentLoginUserTickit(user.getAccess_token());
                    setResult(RESULT_OK);
                    finish();
                }
            });
        }else{
            Intent in =new Intent(ChangeAccountActivity.this,LoginAccountActivity.class);
            in.putExtra("isPay",true);
            startActivity(in);
            finish();
        }
    }


    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.activity_swichaccount;
    }




}
